package com.dvdfe8.crochet.modelo.dao;

import com.dvdfe8.crochet.modelo.entity.Lana;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class LanaDaoImpl implements LanaDao {

    @Autowired
    SessionFactory sessionFactory;
    
    @Override
    @Transactional
    public void crear(Lana lana) {
        sessionFactory.getCurrentSession().saveOrUpdate(lana);
    }

    @Override
    @Transactional
    public List<Lana> listarTodos() {
        String hql = "from Lana";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return query.list();
    }

    @Override
    @Transactional
    public void eliminar(Lana lana) {
        sessionFactory.getCurrentSession().delete(lana);
    }

    @Override
    @Transactional
    public Lana getLanaById(int id) {
        String hql = "from Lana where id=" + id;
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return (Lana) query.list().get(0);
    }

    @Override
    @Transactional
    public Lana getLanaByReferencia(String referencia) {
        String hql = "from Lana where referencia=" + referencia;
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return (Lana) query.list().get(0);
    }

}
